package com.fany.views.circlemenu;

import java.util.ArrayList;

import android.view.View;
import com.fany.fastsearcher.R;
import com.fany.utils.AlgorithmUtils;
import com.fany.utils.BitmapHelper;
import com.fany.utils.ShapeUtils;
import com.fany.utils.SystemUtils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.graphics.Rect;
import android.view.MotionEvent;
import android.widget.Toast;
import com.fany.views.waterwave.WaterWaveView;

public class CircleMenuView extends View
{
	private CircleMenuItem touchedItem;
	private ArrayList<CircleMenuItem> _items;
	private CircleArea _menuArea;
	private boolean _isMenuTouching;

	private Paint _paint;
	private int _innerRingColor = Color.WHITE;
	private int _outerRingColor = Color.BLUE;

	private CircleArea _controllerArea;

	private boolean _isControllerAimTouching;
	private CircleArea _controllerAimArea;
	private int _controllerAimAngle;
    private CircleArea _controllerButtonArea;
    private boolean _isControllerButtonTouching;


	private PointF _touchPoint;
	private PointF _movePoint;

	public CircleMenuView(Context context)
	{
		super(context);
		_items = new ArrayList<CircleMenuItem>();
		_paint = new Paint();

		int screenWidth = SystemUtils.getScreenWidth();
		int screenHeight = SystemUtils.getScreenHeight();

		_menuArea = new CircleArea();
		_menuArea.adjustArea(new PointF((int) (screenWidth * 0.75), (int) (screenWidth * 0.25)),
				(int) (screenWidth * 0.5));

		_controllerArea = new CircleArea();
		_controllerArea.adjustArea(new PointF((int) (screenWidth * 0.3), (int) (screenHeight * 0.6)),
				(int) (screenWidth * 0.25));

		_controllerAimArea = new CircleArea();
		adjustControllerAimArea(0);

		_controllerButtonArea = new CircleArea();
		_controllerButtonArea.adjustArea(_controllerArea.centerPoint, (_controllerArea.radius - 90));
	}

	private void adjustControllerAimArea(int angle)
	{
		_controllerAimAngle = angle;
		PointF center = AlgorithmUtils.calculateCirclePoint(_controllerArea.centerPoint, _controllerArea.radius, angle);
		final int controllerAimRadius = 60;
		_controllerAimArea.adjustArea(center, controllerAimRadius);
	}

	public void addItem(String Text, int resourceId, int commandId)
	{
		CircleMenuItem item = new CircleMenuItem(_menuArea);
		_items.add(item);
		item.setBitmap(BitmapHelper.loadBitmapResource(resourceId));
		item.text = Text;
		item.commandId = commandId;
		adjustItemAngle();
	}

	private void adjustItemAngle()
	{
		int angle = 0;
		int degreeDelta = 360 / _items.size();
		for (int index = 0; index < _items.size(); index++)
		{
			_items.get(index).setAngle(angle);
			angle += degreeDelta;
			_items.get(index).adjustItemCoordinate();
		}
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent e)
	{
		switch (e.getAction() & MotionEvent.ACTION_MASK)
		{
			case MotionEvent.ACTION_DOWN:
				for (int i = 0; i < _items.size(); i++)
				{
					if (_items.get(i).isPointInMenu(new PointF(e.getX(), e.getY())))
					{
						touchedItem = _items.get(i);
						break;
					}
				}
				_isMenuTouching = _menuArea.isPointInArea(e.getX(), e.getY());
				_isControllerAimTouching = _controllerAimArea.isPointInArea(e.getX(), e.getY());
				_isControllerButtonTouching = _controllerButtonArea.isPointInArea(e.getX(), e.getY());
				_touchPoint = new PointF(e.getX(0), e.getY(0));
				_movePoint = new PointF(e.getX(0), e.getY(0));
				return _isMenuTouching || _isControllerAimTouching || _isControllerButtonTouching;
			case MotionEvent.ACTION_MOVE:
				double distance = AlgorithmUtils.calculateDistance(new PointF(e.getX(0), e.getY(0)), _touchPoint);
				if (distance > 50)
				{
					changeFocusItem(e.getX(), e.getY());
					if (_isMenuTouching)
					{
						resetItemsAngle(e.getX(), e.getY());
						invalidate();
					}
					if (_isControllerAimTouching)
					{
						resetControllerAimAngle(e.getX(), e.getY());
						invalidate();
					}
				}
				_movePoint = new PointF(e.getX(0), e.getY(0));
				break;
			case MotionEvent.ACTION_UP:
				_isMenuTouching = false;
				_isControllerAimTouching = false;
				if (_isControllerButtonTouching)
				{
					if (_controllerButtonArea.isPointInArea(e.getX(), e.getY()))
					{
						performClickFocusItem();
					}
				}
				_isControllerButtonTouching = false;
				break;
		}
		return false;
	}

	private void changeFocusItem(float x, float y)
	{
		if (_items.size() == 0)
		{
			return;
		}
		int angleOffset = 360 / _items.size() / 2;
		int angle = 90 + (int) AlgorithmUtils.calculateTriangleAngle(_menuArea.centerPoint.x
				- _controllerArea.centerPoint.x, _controllerArea.centerPoint.y - _menuArea.centerPoint.y);
		for (CircleMenuItem item : _items)
		{
			item.focused = (Math.abs(item.getAngle() - angle) % 360) < angleOffset;
		}
	}

	private void performClickFocusItem()
	{
		for (CircleMenuItem item : _items)
		{
			if (item.focused)
			{
				touchedItem = item;
				break;
			}
		}
		if (touchedItem == null)
		{
			Toast.makeText(this.getContext(), "û�в˵���ѡ��", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public void onDraw(Canvas canvas)
	{
        super.onDraw(canvas);
		drawRing(canvas);

		for (int index = 0; index < _items.size(); index++)
		{
			CircleMenuItem item = _items.get(index);
			if (item.visible)
			{
				drawItem(canvas, item);
			}
		}

		drawController(canvas);
	}

	private void drawRing(Canvas canvas)
	{
		_paint.setAntiAlias(true);
		_paint.setColor(_innerRingColor);
		_paint.setStyle(Paint.Style.FILL);
		_paint.setAlpha(0x30);
		canvas.drawCircle(_menuArea.centerPoint.x, _menuArea.centerPoint.y, _menuArea.radius - 80, _paint);

		_paint.setAntiAlias(true);
		_paint.setColor(_outerRingColor);
		_paint.setStyle(Paint.Style.FILL);
		_paint.setAlpha(0x30);
		canvas.drawCircle(_menuArea.centerPoint.x, _menuArea.centerPoint.y, _menuArea.radius + 40, _paint);
	}

	void drawItem(Canvas canvas, CircleMenuItem item)
	{
		_paint.setColor(Color.WHITE);
		_paint.setTextSize(SystemUtils.getScreenWidth() / 18);
		Rect itemRect = item.getItemRect();
        Bitmap bitmap = item.getBitmap();
		if (item.focused)
		{
			ShapeUtils.scaleRect(itemRect, 1.6f);
			_paint.setTextSize(SystemUtils.getScreenWidth() / 15);
		}

		canvas.drawBitmap(bitmap, null, itemRect, null);
		Rect rect = new Rect();
		_paint.getTextBounds(item.text, 0, item.text.length(), rect);
		canvas.drawText(item.text, item.x - rect.width() / 2, itemRect.bottom + rect.height(), _paint);
	}

	private void drawController(Canvas canvas)
	{
		_paint.setAntiAlias(true);
		_paint.setColor(_innerRingColor);
		_paint.setStyle(Paint.Style.FILL);
		_paint.setAlpha(0x30);
		canvas.drawCircle(_controllerArea.centerPoint.x, _controllerArea.centerPoint.y, _controllerArea.radius - 90,
				_paint);

		_paint.setAntiAlias(true);
		_paint.setColor(_outerRingColor);
		_paint.setStyle(Paint.Style.FILL);
		_paint.setAlpha(0x30);
		canvas.drawCircle(_controllerArea.centerPoint.x, _controllerArea.centerPoint.y, _controllerArea.radius, _paint);

		Bitmap bitmap = BitmapHelper.loadBitmapResource(R.drawable.ok) ;
        bitmap = BitmapHelper.toRoundCorner(bitmap, bitmap.getHeight());
		canvas.drawBitmap(bitmap, null, _controllerButtonArea.area, null);

		bitmap = BitmapHelper.loadBitmapResource(R.drawable.ring);
		canvas.drawBitmap(bitmap, null, _controllerAimArea.area, null);
	}

	private void resetItemsAngle(double x, double y)
	{
		int startAngle = AlgorithmUtils.computeAngle(_menuArea.centerPoint, _movePoint.x, _movePoint.y);
		int endAngle = AlgorithmUtils.computeAngle(_menuArea.centerPoint, x, y);
		int angle = endAngle - startAngle;
		for (CircleMenuItem item : _items)
		{
			item.setAngle(item.getAngle() + angle);
			item.adjustItemCoordinate();
		}
	}

	private void resetControllerAimAngle(double x, double y)
	{
		int startAngle = AlgorithmUtils.computeAngle(_controllerArea.centerPoint, _movePoint.x, _movePoint.y);
		int endAngle = AlgorithmUtils.computeAngle(_controllerArea.centerPoint, x, y);
		int angle = endAngle - startAngle;
		adjustControllerAimArea(_controllerAimAngle + angle);
		for (CircleMenuItem item : _items)
		{
			item.setAngle(item.getAngle() + angle);
			item.adjustItemCoordinate();
		}
	}

	public CircleMenuItem getTouchedItem()
	{
		return touchedItem;
	}

	public void cleanTouchedItem()
	{
		touchedItem = null;
	}

	public void setRingColor(int innerRing, int outerRing)
	{
		_innerRingColor = innerRing;
		_outerRingColor = outerRing;
	}
}
